package com.ulic.customerManApplication.membermodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.mailmodule.bo.MailRecord;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.membermodule.dao.MemberDao;
import com.ulic.customerManApplication.membermodule.vo.MemberVO;
import com.ulic.customerManApplication.messagemodule.bo.MessageRecord;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
@Component("memberDao")
public class MemberDaoImpl extends BaseDao implements MemberDao {
    
	Session session = null;
	@Override
	public void deleteMember(Member member) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		if (member.getMessageRecords().size() != 0) {
			for (MessageRecord messageRecord : member.getMessageRecords()) {
				messageRecord.getReceiver().remove(member);
			}
		}
		if (member.getMailRecordForCopy().size() != 0) {
			for (MailRecord mailRecord : member.getMailRecordForCopy()) {
				mailRecord.getCopiesTo().remove(member);
			}
		}
		if(member.getMailRecordsForAddr().size()!=0){
			for (MailRecord maRecord : member.getMailRecordsForAddr()) {
				maRecord.getAddressees().remove(member);
			}
		}
		session.delete(member);
		session.flush();
		
	}

	@Override
	public Member getMember(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List list = cri.add(Restrictions.eq("id", id)).list();
		Member member = null;
		if(list.size()>0){
			member	= (Member)list.get(0);
		}
		return member;
	}


	@Override
	public PagerModel getMembers(MemberVO memberVO, int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Member member where 1=1");
		Member member = memberVO.getMember();
		if(member != null){
			if(member.getGender()!=null && ! "No".equals(member.getGender())){
				sb.append(" and member.gender = '"+member.getGender()+"'");
			}
			if(member.getSubCompany()!= null&&! "No".equals(member.getSubCompany()) ){
				sb.append(" and member.subCompany= '"+member.getSubCompany()+"'");
			}
			if(member.getSubCity()!= null &&! "No".equals(member.getSubCity())){
				sb.append(" and member.subCity = '"+member.getSubCity()+"'");
			}
			if(member.getMemberType()!=null && ! "No".equals(member.getMemberType())){
				sb.append(" and member.memberType = '"+member.getMemberType()+"'");
			}
			if(member.getMemberName()!=null && member.getMemberName().length()!=0){
				sb.append(" and member.memberName like '%"+member.getMemberName()+"%'");
			}
			if(member.getCarBrand()!=null && member.getCarBrand().length()!=0){
				sb.append(" and member.carBrand like '%"+member.getCarBrand()+"%'");
			}
			if(memberVO.getStartbirthday()!=null &&memberVO.getStartbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+memberVO.getStartbirthday()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndbirthday()!=null &&memberVO.getEndbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+memberVO.getEndbirthday()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			
			if(memberVO.getStartcreate()!=null &&memberVO.getStartcreate().length()!=0){
				sb.append(" and member.createDate>= to_date('"+memberVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndcreate()!=null &&memberVO.getEndcreate().length()!=0){
				sb.append(" and member.createDate<= to_date('"+memberVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by member.donateStuNum desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}
	
	@Override
	public void insertMember(Member member) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		try{
			session.save(member);
		}catch (Exception e) {
			e.printStackTrace();
		}
		session.flush();
	}

	@Override
	public void insertMembers(List<Member> members) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(Member member:members){
			session.save(member);
		}
		session.flush();
	}

	@Override
	public void updateMember(Member member) throws Exception {
        try{
        	session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        	session.merge(member);
        	session.flush();
        }catch (Exception e) {
        	e.printStackTrace();
		}
	}

	@Override
	public PagerModel getMembers(int curPage) {
		String sql = " from Member member order by createDate desc";
		return searchPaginated(sql, curPage, CONSTANT.PageSize);
	}

	@Override
	public List<Member> getMembers(User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List list = cri.createAlias("user", "u").add(Restrictions.eq("u.um_user_id", user.getUm_user_id())).list();
		return list;
	}

	@Override
	public Member getMember(int type ,String condition) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Member.class);
		List list = null;
		if(type==1){
			list = cri.add(Restrictions.eq("idCard", condition)).list();
		}
		if(type==2){
		    list = cri.add(Restrictions.eq("cellPhone", condition)).list();
		}
		Member member = null;
		if(list.size()>0){
			member	= (Member)list.get(0);
		}
		return member;
	}

	@Override
	public PagerModel getMembers(MemberVO memberVO, int curPage, int totalNum) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Member member where 1=1");
		Member member = memberVO.getMember();
		if(member != null){
			if(member.getGender()!=null && ! "No".equals(member.getGender())){
				sb.append(" and member.gender = '"+member.getGender()+"'");
			}
			if(member.getSubCompany()!= null&&! "No".equals(member.getSubCompany()) ){
				sb.append(" and member.subCompany= '"+member.getSubCompany()+"'");
			}
			if(member.getSubCity()!= null &&! "No".equals(member.getSubCity())){
				sb.append(" and member.subCity = '"+member.getSubCity()+"'");
			}
			if(member.getMemberType()!=null && ! "No".equals(member.getMemberType())){
				sb.append(" and member.memberType = '"+member.getMemberType()+"'");
			}
			if(member.getMemberName()!=null && member.getMemberName().length()!=0){
				sb.append(" and member.memberName like '%"+member.getMemberName()+"%'");
			}
			if(member.getCarBrand()!=null && member.getCarBrand().length()!=0){
				sb.append(" and member.carBrand like '%"+member.getCarBrand()+"%'");
			}
			if(member.getRemark()!=null && member.getRemark().length()!=0){
				sb.append(" and member.remark like '%"+member.getRemark()+"%'");
			}
			if(memberVO.getStartbirthday()!=null &&memberVO.getStartbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+memberVO.getStartbirthday()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndbirthday()!=null &&memberVO.getEndbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+memberVO.getEndbirthday()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			
			if(memberVO.getStartcreate()!=null &&memberVO.getStartcreate().length()!=0){
				sb.append(" and member.createDate>= to_date('"+memberVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndcreate()!=null &&memberVO.getEndcreate().length()!=0){
				sb.append(" and member.createDate<= to_date('"+memberVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by member.donateStuNum desc");
		return searchPaginated(sb.toString(), curPage, totalNum);
	}

	@Override
	public List<Object> getMembers(MemberVO memberVO) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from Member member where 1=1");
		Member member = memberVO.getMember();
		if(member != null){
			if(member.getGender()!=null && ! "No".equals(member.getGender())){
				sb.append(" and member.gender = '"+member.getGender()+"'");
			}
			if(member.getSubCompany()!= null&&! "No".equals(member.getSubCompany()) ){
				sb.append(" and member.subCompany= '"+member.getSubCompany()+"'");
			}
			if(member.getSubCity()!= null &&! "No".equals(member.getSubCity())){
				sb.append(" and member.subCity = '"+member.getSubCity()+"'");
			}
			if(member.getMemberType()!=null && ! "No".equals(member.getMemberType())){
				sb.append(" and member.memberType = '"+member.getMemberType()+"'");
			}
			if(member.getMemberName()!=null && member.getMemberName().length()!=0){
				sb.append(" and member.memberName like '%"+member.getMemberName()+"%'");
			}
			if(member.getCarBrand()!=null && member.getCarBrand().length()!=0){
				sb.append(" and member.carBrand like '%"+member.getCarBrand()+"%'");
			}
			if(member.getRemark()!=null && member.getRemark().length()!=0){
				sb.append(" and member.remark like '%"+member.getRemark()+"%'");
			}
			if(memberVO.getStartbirthday()!=null &&memberVO.getStartbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+memberVO.getStartbirthday()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndbirthday()!=null &&memberVO.getEndbirthday().length()!=0){
				sb.append(" and to_date(member.birthday,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+memberVO.getEndbirthday()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			
			if(memberVO.getStartcreate()!=null &&memberVO.getStartcreate().length()!=0){
				sb.append(" and member.createDate>= to_date('"+memberVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(memberVO.getEndcreate()!=null &&memberVO.getEndcreate().length()!=0){
				sb.append(" and member.createDate<= to_date('"+memberVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by member.donateStuNum desc");
		session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		Query q  = session.createQuery(sb.toString());
		return q.list();
	}


}
